$(document).ready(function(){
	$.getScript("https://www.google.com/jsapi",function(){
		setTimeout(function()
		{
			google.load('visualization', '1', {'callback':'', 'packages':['corechart']});
		}, 1);		
	});
	
	$("#company_infos").load('/companies/show');
	$("#project_show").load('showall');
	
	$("#addProject").click(function(){
		$("#project_edit").load('new');
	});
	
	$(document).on("click", "#newProject", function(){
		var items=$("#projname").val() + "," + $("#projsprintlength").val() ;		
		$.getJSON("../projects/create?items="+items).done(function(data){
			if( data != 0 ){
				var name = $("#projname").val();
				var html = "<div class='project' id='proj_id_"+data+"'>Name: "+name+"</div>";
				$("#project_show").append(html);
			}
			else
			{
				alert("error occured...");
			}
		});
	});
	
	var marked_id=0;
	$(document).on("click", ".project", function(){
		var proj_id = this.id;		
		if ( proj_id.split("_")[2] != marked_id )
		{
			if( marked_id > 0 ){
				$("#proj_id_"+marked_id).removeClass("marked");
			}
			$(this).addClass("marked");
			var proj_entities = $(this).children();
			marked_id = proj_id.split("_")[2];
			$("#project_edit").load('edit?id='+proj_id, function(){
				// ULTRA DIRTY, DA NICHT EXPLIZIT GETRIGGERT!
				if ( $("#piechart") )
				{
					var hidden = $("#chartValues").val().split("_");
					var gesamt = parseInt(hidden[1].trim());
					var open = parseInt(hidden[2].trim());
					var verworfen = parseInt(hidden[3].trim());
					var in_sprintbl = parseInt(hidden[4].trim());
					var ongoing = parseInt(hidden[5].trim());
					var rating = parseInt(hidden[6].trim());
					var done = parseInt(hidden[7].trim());
					var new_rating = parseInt(hidden[8].trim());
					var chartdata = [['Stories', 'Count States'], ['Offen', open], ['Verworfen', verworfen], ['In Sprintbacklog', in_sprintbl], ['Ongoing', ongoing], ['Zur Bewertung', rating], ['Fertig', done], ['Neubewertung', new_rating] ];
					drawChart(chartdata);
				}
				
				if( $("#sprint_backlog_config") ){
					// SPRINT COLUMNS
					$("#sprint_backlog_config").load('../sprint_backlog_columns/show');
				}
			});		
		}
		else
		{
			marked_id = 0;
			$(this).removeClass("marked");
		}
	});
	
	$(document).on("click", "#updatebutton", function(){
		var cid = $("#company_id").val();
		var cname = $("#company_name").val();
		var caddress = $("#company_address").val();
		var caccount = $("#company_account").val();

		var items = cid + "," + cname + "," + caddress +"," + caccount; 
		$.getJSON("../companies/update?items="+items).done(function(data){
			if ( data == 0 )
			{
				$("#company_name").val(cname);
				$("#company_address").val(caddress);
				$("#company_account").val(caccount);
			}
			else
			{
				alert("error");
			}
		});
	});
	
	$("#deleteProject").click(function(){
		showDialog('warning', "L&ouml;schen des Projektes", "Das Projekt wird unwiederruflich gelöscht!<p><a href=\"javascript:void(null)\" id=\"confirmProjectDelete\"><img alt=\"confirmIcon\" class=\"icon\" id=\"confirmIcon\" src=\"/assets/layout/OK.png\"/> Best&auml;tigen</a><a href=\"javascript:void(null)\" id=\"cancel\" class=\"closeButton\"><img alt=\"cancelIcon\" class=\"icon\" id=\"cancelIcon\" src=\"/assets/layout/Cancel.png\"/> Abbrechen</a></p>");
	});
	
	$(document).on("click", "#confirmProjectDelete", function(){
		$("#shader").hide();	
		$.getJSON("../projects/destroy?items="+marked_id).done(function(data){		
			if( data == 0 ){
				$("#proj_id_"+marked_id).hide();
			}
			else{
				alert("Fehler beim L&ouml;schen.");
			}
		});
	});
	
	// mal wieder richtig dirty
	var noToggle = 0;
	$(document).on("click","#addSprintCol", function(){
		var colname = $("#new_sprint_col_name").val();
		var request = marked_id + "," + colname;
		if(colname!=""){
			$.get("../sprint_backlog_columns/new?items="+request).done(function(data){
				if(data!=0){
					$("#sprint_columns_table").append(data);
					$("#new_sprint_col_name").val("");
				}
				else{
					showDialog("error", "Ups..", "Da lief etwas gewaltig schief..");
				}
				noToggle = 0;
			});
		}
		else{
			showDialog("error", "Spaltenname ist leer", "Bitte geben Sie einen Spaltennamen ein.");
		}
	});
	
	// fieser fieser workaround...aber ich bin so faul...
	$(document).on("click", "#new_sprint_col_name",function(){
		noToggle=1;
	});
	
	$(document).on("click", "#addSprintCol",function(){
		noToggle=1;
	});	
	
	$(document).on("click", "#proj_max_user_stories",function(){
		noToggle=1;
	});	
	
	$(document).on("click","#toggleSprintBacklog", function(){
		if ( noToggle == 0){
			if($("#toggleSprintBacklog img").attr("src") == "/assets/layout/Plus.png")
				$("#toggleSprintBacklog img").attr("src","/assets/layout/Minus.png");
			else
				$("#toggleSprintBacklog img").attr("src","/assets/layout/Plus.png");
			
			$("#sprint_backlog_config").slideToggle("fast");			
		}
	});
	
	$(document).on("click","#toggleProjectSummary", function(){

		if($("#toggleProjectSummary img").attr("src") == "/assets/layout/Plus.png")
			$("#toggleProjectSummary img").attr("src","/assets/layout/Minus.png");
		else
			$("#toggleProjectSummary img").attr("src","/assets/layout/Plus.png");
		
		$(".project_summary").slideToggle("fast");
	});
	
	$(document).on("click","#toggleChartWrapper", function(){
		
		if($("#toggleChartWrapper img").attr("src") == "/assets/layout/Plus.png")
			$("#toggleChartWrapper img").attr("src","/assets/layout/Minus.png");
		else
			$("#toggleChartWrapper img").attr("src","/assets/layout/Plus.png");
		
		$(".chart_wrapper").slideToggle("fast");
	});
	
	$(document).on("click", ".user_story_max_save", function(){
		var stories = $("#proj_max_user_stories").val();
		var request = marked_id + "," + stories;
		$.get("../projects/update?items="+request).done(function(data){
			if ( data == 0 ){
				showDialog("notice", "Speichern erfolgreich", "Die &Auml;nderungen wurden erfolgreich gespeichert.");
			}
			else{
				showDialog("error", "Speichern fehlgeschalgen", "Die &Auml;nderungen konnten nicht gespeichert werden.");
			}
			noToggle = 0;
		});
	});
});

// chartdata
// 	 [['Stories', 'Count States'],
// ['Offen', 11], 
// ['In--leer--Bearbeitung', 2],          
// ['Fertig', 2],          
// ['Zu Bewertung', 2]] 

function drawChart(chartdata) {        
	 var data = google.visualization.arrayToDataTable(chartdata);        

	 var options = { title: 'User Stories Status' };

	 var chart = new google.visualization.PieChart(document.getElementById('piechart'));        
	 chart.draw(data, options);      
}    
